#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# 复制文件到目标目录
function copy_files() {
    # 遍历源目录中的文件（包括隐藏文件）
    for file in "$1"/* "$1"/.*; do
        if [[ -f "$file" ]]; then
            # 如果是文件，则复制到目标目录
            cp "$file" "$2"
        elif [[ -d "$file" ]] && [[ ! "$file" == "$1/." ]] && [[ ! "$file" == "$1/.." ]]; then
            # 如果是目录（排除"."和".."），则递归调用创建子目录并复制文件
            sub_dir=$(basename "$file")
            mkdir -p "$2/$sub_dir"
            copy_files "$file" "$2/$sub_dir"
        fi
    done
}

function do_copy_files(){
    if [[ $# -ne 2 ]]; then
        echo "请提供源目录和目标目录作为参数"
	exit 1
    fi

    # 源目录
    source_dir=$1
    # 目标目录
    target_dir=$2
	
    # 创建目标目录（如果不存在）
    if [[ ! -d "$target_dir" ]]; then
        mkdir -p "$target_dir"
    fi

    # 复制文件到目标目录
    copy_files "$source_dir" "$target_dir"
}

do_copy_files $@

exit $?

#eof
